import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Welcome from '../views/Welcome.vue'
import Category from '../views/Category.vue'
import Products from '../views/Products.vue'
import Bar from '../views/Bar.vue'
import Line from '../views/Line.vue'
import Pie from '../views/Pie.vue'
import User2 from '../views/User2.vue'
import Dingg from '../views/Dingg.vue'
import Dinghui from '../views/Dinghui.vue'
import store from '@/store'
Vue.use(VueRouter)
/* 第一步 */
const one = {
	path: 'role',
	name: 'role',
	meta: {
		title: '角色管理'
	},
	component: () => import('../views/Role.vue')
}
const two = {
	path: '*',
	name: 'NotFound',
	component: () => import('../views/NotFound.vue')
}
const three = {
		path: 'user',
		name: 'user',
		meta: {
			title: '用户管理'
		},
		component: () => import('../views/User.vue')
	}
	/* 第二步 */
	const map = {
		'/role': one,
		'/user': three
	}
const routes = [{
		path: '/',
		name: 'Home',
		component: Home,
		redirect: '/welcome',
		children: [{
				path: 'welcome',
				name: 'welcome',
				component: Welcome
			},
			{
				path: 'category',
				name: 'category',
				meta: {
					title: '品类管理'
				},
				component: Category
			},
			{
				path: 'product',
				name: 'product',
				meta: {
					title: '商品生产'
				},
				component: Products
			},
			{
				path: 'charts/bar',
				name: 'bar',
				meta: {
					title: '柱状图'
				},
				component: Bar
			},
			{
				path: 'charts/line',
				name: 'line',
				meta: {
					title: '折线图'
				},
				component: Line
			},
			{
				path: 'charts/pie',
				name: 'pie',
				meta: {
					title: '饼图'
				},
				component: Pie
			},
			{
				path: 'user2',
				name: 'user2',
				meta: {
					title: '用户管理2'
				},
				component: User2
			},
			{
				path: 'dingg',
				name: 'dingg',
				meta: {
					title: '订单管理'
				},
				component: Dingg
			},
			{
				path: 'ding/hui',
				name: 'dinghui',
				meta: {
					title: '订单汇总'
				},
				component: Dinghui
			},
		]
	},
	{
		path: '/about',
		name: 'About',
		component: () => import('../views/About.vue')
	},
	{
		path: '/login',
		name: 'Login',
		component: () => import('../views/Login.vue')
	},



]

const router = new VueRouter({
	routes,
	mode: 'history'
})
/* 第三步 */
export function initDnameicRoutes() {
	console.log(router);
	console.log(store);
	const currentRoues = router.options.routes
	if (store.state.user.navBar) {
		var rightlist = store.state.user.navBar.list
		if (rightlist) {
			rightlist.forEach(item => {
				if (item.name == '首页') {
					item.leftMenus.forEach(child => {
						console.log(child);
						const temp = map[child.key]
						console.log(temp);
						if (temp) {
							temp.meta.ah = child.rights
							currentRoues[0].children.push(temp)
							console.log(currentRoues[0].children);
							currentRoues[0].children.forEach(v => {
								router.addRoute('Home', v)
							})

						}
					})
				}
			})
		}
		currentRoues.push(two)
		router.addRoute(two)
		console.log(currentRoues);
	}
}
router.beforeEach((to, from, next) => {
	let token = window.sessionStorage.getItem('token')
	if (to.path === '/login') {
		next()
	} else {
		if (!token) {
			next('/login')
		} else {
			next()
		}
	}
})
//解决vue路由重复导航错误
//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}
export default router
